package com.hfzy.ihk.web.userCenter.support.security;

import com.hfzy.ihk.common.web.enums.LoginErrorMsgEnum;
import com.hfzy.ihk.facade.userCenter.entity.UserInfo;
import com.hfzy.ihk.facade.userCenter.service.IUserService;
import com.hfzy.ihk.facade.userCenter.vo.LoginUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author wws
 */
@Service("userDetailsService")
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    IUserService userService;

    @Value("${spring.profiles.active}")
    String profiles;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        log.info("====>校验操作=====start>username:{}",username);
        UserInfo user = userService.findUserByUserName(username);
        if (user == null) {
            throw new BadCredentialsException(LoginErrorMsgEnum.USER_NOT_FIND.toStr());
        }
        log.info("====>校验操作=====end>username:{}",username);
        return new LoginUserInfo(user);
    }


}


